﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Robotics.Race
{
    public partial class frmMain : Form
    {
		List<CarPanel> carPanels = new List<CarPanel>();
        private Rules rules = new Rules();

        public frmMain()
        {
            DirectInputWrapper.Initialize(this);
            InitializeComponent();
            RefreshRules();
            
            GlobalSettings.Load();
            pgGlobalSettings.SelectedObject = GlobalSettings.Instance;
        }

        public double GetConstraintedSpeed(int distance)
        {
            // FIXME
            return Math.Max(distance * 2 - 5, 0);
        }

        private void btAddCar_Click(object sender, EventArgs e)
        {
            CarPanel panel = new CarPanel();
            panel.Dock = DockStyle.Top;
			panel.Parent = panelCars;
            panel.Car.SetRules(rules);
			panelCars.Controls.SetChildIndex( panel, 1 );
			carPanels.Add( panel );
        }

		private void btClearCars_Click( object sender, EventArgs e )
		{
            ClearCars();
		}

        public void ClearCars()
        {
            foreach (CarPanel panel in carPanels)
            {
                panel.CloseThread();
                panel.Dispose();
            }
            carPanels.Clear();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Up ||
                keyData == Keys.Down ||
                keyData == Keys.Right ||
                keyData == Keys.Left ||
                keyData == Keys.Space ||
                keyData == Keys.Tab ||
                keyData == Keys.PageDown ||
                keyData == Keys.PageUp)
                return true;
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            ClearCars();
            GlobalSettings.Save();
        }

        private void chbTouchRuleEnabled_CheckedChanged(object sender, EventArgs e)
        {
            rules.TouchRule.Enabled = chbTouchRule.Checked;
        }

        private void chbSonarRule_CheckedChanged(object sender, EventArgs e)
        {
            rules.SonarRule.Enabled = chbSonarRule.Checked;
        }

        private void nudSonarRuleDistance_ValueChanged(object sender, EventArgs e)
        {
            rules.SonarRule.Distance = (int)nudSonarRuleDistance.Value;
        }

        public void RefreshRules()
        {
            rules.SonarRule.Enabled = chbSonarRule.Checked;
            rules.SonarRule.Distance = (int)nudSonarRuleDistance.Value;
            rules.TouchRule.Enabled = chbTouchRule.Checked;
            rules.TerrainRule.Enabled = chbTerrain.Checked;
            rules.TerrainRule.GrassRatio = (int)nudGrassTerrainRatio.Value;
            rules.TerrainRule.GrassValue = (int)nudGrassTerrain.Value;
            rules.TerrainRule.RoadRatio = (int)nudRoadTerrainRatio.Value;
            rules.TerrainRule.RoadValue = (int)nudRoadTerrain.Value;
        }

        private void nudRoadTerrain_ValueChanged(object sender, EventArgs e)
        {
            rules.TerrainRule.RoadValue = (int)nudRoadTerrain.Value;
        }

        private void nudGrassTerrain_ValueChanged(object sender, EventArgs e)
        {
            rules.TerrainRule.GrassValue = (int)nudGrassTerrain.Value;
        }

        private void chbTerrainEnabled_CheckedChanged(object sender, EventArgs e)
        {
            rules.TerrainRule.Enabled = chbTerrain.Checked;
        }

        private void nudRoadTerrainRatio_ValueChanged(object sender, EventArgs e)
        {
            rules.TerrainRule.GrassRatio = (int)nudGrassTerrainRatio.Value;
        }

        private void nudGrassTerrainRatio_ValueChanged(object sender, EventArgs e)
        {
            rules.TerrainRule.RoadRatio = (int)nudRoadTerrainRatio.Value;
        }
    }
}
